package com.husd.framework.autocode;

import com.husd.framework.a_autocode_new.scanner.StringListScanner;
import com.husd.framework.a_autocode_new.util.WordUtil;
import com.husd.framework.autocode.dto.EbayWord;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;

/**
 * @author shengdonghu@126.com
 * @date 2024/1/25 11:22
 */
public class Ebay011Convert2EbayWord {

    private AtomicLong idGen = new AtomicLong(0L);
    List<String> stringList;
    StringListScanner stringListScanner = null;
    List<EbayWord> wordList = new ArrayList<>();

    public Ebay011Convert2EbayWord(List<String> stringList) {
        this.stringList = stringList;
        this.stringListScanner = new StringListScanner(stringList);
    }

    // 要把注释内容解析出来
    // 要把连续的字符串合并 例如： <abc> this is a word </abc> 要返回3个元素
    // <abc>
    // this is a word
    // </abc>
    public List<EbayWord> parse() {

        while (stringListScanner.haveWord()) {
            String current = stringListScanner.current();
            String pre = stringListScanner.getPreWord();
            switch (current) {
                case " ":
                case "\t":
                case "\n":
                    break;
                case "<":
                    boolean comment = isComment();
                    if (comment) {
                        //移动指针到注释结束的后面一个字符
                        String commentContent = readCommentContent();
                        addWord(EbayWord.C1);
                        addWord(new EbayWord(commentContent, EbayWord.EbayWorkType.WORD));
                        addWord(EbayWord.C2);
                        stringListScanner.next(); // skip -- now is >
                        stringListScanner.next(); // skip -- now is >
                    } else {
                        addWord(EbayWord.L1);
                    }
                    break;
                case "/":
                    addWord(EbayWord.XIE);
                    break;
                case ">":
                    addWord(EbayWord.R1);
                    break;
                case "=":
                    addWord(EbayWord.EQ);
                    break;
                case "\"":
                    addWord(EbayWord.QUO);
                    break;
                case "'":
                    addWord(EbayWord.QUO);
                    break;
                default:
                    // 这里如果是 <name> this is "adf" name </name> 这种情况 要特殊处理
                    if(WordUtil.isWord(pre,">")) {
                        StringBuilder _sword = new StringBuilder();
                        while(!WordUtil.isWord(stringListScanner.getNextWord(),"<")) {
                            _sword.append(stringListScanner.current());
                            stringListScanner.next();
                        }
                        addWord(new EbayWord(_sword.toString(), EbayWord.EbayWorkType.WORD));
                    } else {
                        String sWord = stringListScanner.current();
                        addWord(new EbayWord(sWord, EbayWord.EbayWorkType.WORD));
                    }
                    break;
            }
            stringListScanner.next();
        }

        return wordList;
    }

    private String readCommentContent() {

        stringListScanner.next(); // now is !
        stringListScanner.next(); // now is --
        stringListScanner.next(); // now is comment
        StringBuilder c = new StringBuilder();
        while (stringListScanner.haveWord() &&
                !(WordUtil.isWord(stringListScanner.getNextWord(), "--")
                        && WordUtil.isWord(stringListScanner.getNextNextWord(), ">"))) {
            c.append(stringListScanner.current());
            c.append(" ");
            stringListScanner.next();
        }
        return c.toString();
    }

    private boolean isComment() {

        return WordUtil.isWord(stringListScanner.getWordByStep(1), "!")
                && WordUtil.isWord(stringListScanner.getWordByStep(2), "--");
    }

    private void addWord(EbayWord ebayWord) {

        long id = idGen.getAndAdd(1L);
        ebayWord.setId(id);
        wordList.add(ebayWord);
    }
}
